const mysqlDB = require('../config/mysqlDB.config.js')
const User = mysqlDB.import('../modules/user')

const transaction = async () => {
  const result = await mysqlDB.transaction(async t => {
    try {
      // return await User.create(
      //   {
      //     name: '刘威'
      //   },
      //   {
      //     transaction: t
      //   }
      // )
      return await User.findOrCreate({
        where: { name: '逗我玩' },
        defaults: {
          name: '逗我玩'
        }
      })
      // create 返回 true
      // find 返回 false
    } catch (error) {
      console.log(error)
    }
  })
  console.log('==========')
  console.log(JSON.parse(JSON.stringify(result)))
}

async function main() {
  await User.sync()
  transaction()
}

main()
